﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Contact;

using Jzy.Model.Contact;
using Jzy.Core.Common.Web;

using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Log;
using Frame.Lib.Core.Log.Operate;
using Frame.Lib.Enums;
using Frame.Lib.Core.Right;
using Frame.Lib.Core.Tree.Dir;
using Jzy.DAL;

public partial class Contact_Outer_Edit :PageEdit
{
    private ContactBO _contact = new ContactBO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (PKID != null)
            {
                DispInfo();
                btnDelete.Visible = true;
            }
            else
            {
                btnDelete.Visible = false;
                IsPublic.Checked = true;
                DirID.Value = Request["DirID"];
                DirName.Text = (new DirBO("ContactDir")).GetName(DirID.Value);
            }
        }
        AddAttr();
    }

    private void AddAttr()
    {
        DirName.ReadOnly = true;
        DirName.Style.Add("cursor", "pointer");
        JSUtil.ShowTreeDlg(DirID, DirName, "ContactDir", "选择目录", true);
        btnDelete.Attributes.Add("onclick", "return confirm('确认删除联系人吗？');");
    }

    private void DispInfo()
    {
        ContactInfo info = _contact.GetInfo(PKID);
        int userID = AppSession.UserID;
        int creatorID = info.CreatorID;

        SystemRightBO right = new SystemRightBO(RightOwnerType.User);
        if (userID != creatorID && userID != Constants.ADMIN_ID && !right.HaveRight(userID, new SystemRight(SystemRightType.联系人, SystemRightName.可修改)))
        {
            Response.Redirect("Detail.aspx?Title=" + HeadTitle + "&ID=" +  StrUtil.EncryptUrl(PKID), true);
        }

        EntityUtil.SetControl(pnlMain, info);
        DirName.Text = (new DirBO(OTableName.ContactDir)).GetName(info.DirID);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ContactInfo info = _contact.GetInfo(PKID);

        if (PKID != null)
        {
            info.ID = int.Parse(PKID);
        }

        EntityUtil.SetEntity(pnlMain, info);

        info.CreatorID = AppSession.UserID;
        info.CreatorName = AppSession.UserName;
        info.InputTime = DateTime.Now;
        info.Orders = 0;
        _contact.Save(info);
        JSUtil.RefreshView();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Logger.Operate(OperateType.Delete, "Contact", "删除了联系人[" + _contact.GetName(PKID) + "]");

        _contact.Delete(PKID);
        JSUtil.RefreshView();
    }
}
